﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Slja.GSMUtility.DAL
{
    /// <summary>
    /// 数据访问类:sms_MsgSend
    /// </summary>
    public partial class sms_MsgSend
    {
        public sms_MsgSend()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Slja.GSMUtility.Model.sms_MsgSend model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.sms_MsgSend(");
            strSql.Append("SendStatus,SendUser,SendTime,RecvNO,MsgContent)");
            strSql.Append(" values (");
            strSql.Append("@SendStatus,@SendUser,@SendTime,@RecvNO,@MsgContent)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@SendStatus", SqlDbType.TinyInt,1),
					new SqlParameter("@SendUser", SqlDbType.Int,4),
					new SqlParameter("@SendTime", SqlDbType.DateTime),
					new SqlParameter("@RecvNO", SqlDbType.NVarChar,20),
					new SqlParameter("@MsgContent", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.SendStatus;
            parameters[1].Value = model.SendUser;
            parameters[2].Value = model.SendTime;
            parameters[3].Value = model.RecvNO;
            parameters[4].Value = model.MsgContent;

            object obj = Slja.DBUtility.SqlServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 增加多条数据
        /// </summary>
        public void Add(List<Slja.GSMUtility.Model.sms_MsgSend> modellist)
        {
            StringBuilder strSql = new StringBuilder("declare @t table(id int);");
            SqlParameter[] parameters = new SqlParameter[modellist.Count*5];
            for (int i = 0; i < modellist.Count; i++)
            {
                Slja.GSMUtility.Model.sms_MsgSend model = modellist[i];
                strSql.Append("insert into dbo.sms_MsgSend(");
                strSql.Append("SendStatus,SendUser,SendTime,RecvNO,MsgContent)");
                strSql.Append(" values (");
                strSql.AppendFormat("@SendStatus{0},@SendUser{0},@SendTime{0},@RecvNO{0},@MsgContent{0});", i);
                strSql.Append("insert into @t(id) select @@identity;"); 
                (parameters[0 + i * 5] = new SqlParameter("@SendStatus" + i.ToString(), SqlDbType.TinyInt, 1)).Value = model.SendStatus;
                (parameters[1 + i * 5] = new SqlParameter("@SendUser" + i.ToString(), SqlDbType.Int, 4)).Value = model.SendUser;
                (parameters[2 + i * 5] = new SqlParameter("@SendTime" + i.ToString(), SqlDbType.DateTime)).Value = model.SendTime;
                (parameters[3 + i * 5] = new SqlParameter("@RecvNO" + i.ToString(), SqlDbType.NVarChar, 20)).Value = model.RecvNO;
                (parameters[4 + i * 5] = new SqlParameter("@MsgContent" + i.ToString(), SqlDbType.NVarChar, 100)).Value = model.MsgContent;
            }
            strSql.Append(" select [id] from @t;");
           DataSet ds = Slja.DBUtility.SqlServerHelper.Query(strSql.ToString(), parameters);
           List<int> ListID = new List<int>();
           for (int k = 0; k < ds.Tables[0].Rows.Count; k++)
           {
               modellist[k].ID = (int)ds.Tables[0].Rows[k]["id"];               
           }
        }
        /// <summary>
        /// 记录短信的发送状态，是否成功。
        /// </summary>
        /// <param name="modellist"></param>
        public void UpdateSendStatus(List<Slja.GSMUtility.Model.sms_MsgSend> modellist)
        {
            StringBuilder sb = new StringBuilder();
            SqlParameter[] parameters = new SqlParameter[modellist.Count * 2];
            for (int i = 0; i < modellist.Count; i++)
            {
                sb.AppendFormat("update dbo.sms_MsgSend set SendStatus=@SendStatus{0} where ID=@ID{0};", i.ToString());
                (parameters[0 + i * 2] = new SqlParameter("@SendStatus" + i.ToString(), SqlDbType.TinyInt, 1)).Value = modellist[i].SendStatus;
                (parameters[1 + i * 2] = new SqlParameter("@ID" + i.ToString(), SqlDbType.Int, 4)).Value = modellist[i].ID;
            }
            Slja.DBUtility.SqlServerHelper.ExecuteSql(sb.ToString(), parameters);
        }
        public List<MsgStatus> GetMsgSendStatus(int[] MsgIDs)
        {
            StringBuilder sb = new StringBuilder("0");
            for (int i = 0; i < MsgIDs.Length; i++)
            {
                if (MsgIDs[i] !=0)
                {
                    sb.Append(","+MsgIDs[i].ToString());
                }
            }
            string sql = "select ID,SendStatus,RecvNO from dbo.sms_MsgSend where id in("+sb.ToString()+") ";
            DataSet ds = Slja.DBUtility.SqlServerHelper.Query(sql);
            List<MsgStatus> ListStatus = new List<MsgStatus>();
            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
            {
                ListStatus.Add(new MsgStatus((int)ds.Tables[0].Rows[j]["ID"],
                                (byte)ds.Tables[0].Rows[j]["SendStatus"],
                                ds.Tables[0].Rows[j]["RecvNO"].ToString()
                                )
                    );
            }
            return ListStatus;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Slja.GSMUtility.Model.sms_MsgSend GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,SendStatus,SendUser,SendTime,RecvNO,MsgContent from sms_MsgSend ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            Slja.GSMUtility.Model.sms_MsgSend model = new Slja.GSMUtility.Model.sms_MsgSend();
            DataSet ds = Slja.DBUtility.SqlServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                AssignModel(ds.Tables[0].Rows[0], model);
                return model;
            }
            else
            {
                return null;
            }
        }
        public List<Slja.GSMUtility.Model.sms_MsgSend> GetWaitingSendMsg()
        {
            //return GetModelList("SendStatus=0");
            //sp_getWaitingSendMsg
            List<Slja.GSMUtility.Model.sms_MsgSend> list = new List<Model.sms_MsgSend>();
            Slja.GSMUtility.Model.sms_MsgSend model;
            DataSet ds = Slja.DBUtility.SqlServerHelper.RunProcedure("dbo.sp_getWaitingSendMsg", new SqlParameter[0], "WaitingSendMsg");
            for (int i = 0; i < ds.Tables["WaitingSendMsg"].Rows.Count; i++)
            {
                model = new Model.sms_MsgSend();
                AssignModel(ds.Tables["WaitingSendMsg"].Rows[i], model);
                list.Add(model);
            }
            return list;
        }
        public List<Slja.GSMUtility.Model.sms_MsgSend> GetModelList(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,SendStatus,SendUser,SendTime,RecvNO,MsgContent from dbo.sms_MsgSend ");
            if (!string.IsNullOrEmpty(sqlWhere))
            {
                strSql.Append(" where ");
                strSql.Append(sqlWhere);
            }           
         
            List<Slja.GSMUtility.Model.sms_MsgSend> list = new List<Model.sms_MsgSend>();
             Slja.GSMUtility.Model.sms_MsgSend model ;
            DataSet ds = Slja.DBUtility.SqlServerHelper.Query(strSql.ToString());
            for (int i=0;i<ds.Tables[0].Rows.Count;i++)
            {
                model= new Slja.GSMUtility.Model.sms_MsgSend();
                AssignModel(ds.Tables[0].Rows[i], model);
                list.Add(model);
            }
            return list;
        }
        private void AssignModel(DataRow dr ,Slja.GSMUtility.Model.sms_MsgSend model)
        {
            if (dr["ID"] != null && dr["ID"].ToString() != "")
            {
                model.ID = int.Parse(dr["ID"].ToString());
            }
            if (dr["SendStatus"] != null && dr["SendStatus"].ToString() != "")
            {
                model.SendStatus = uint.Parse(dr["SendStatus"].ToString());
            }
            if (dr["SendUser"] != null && dr["SendUser"].ToString() != "")
            {
                model.SendUser = int.Parse(dr["SendUser"].ToString());
            }
            if (dr["SendTime"] != null && dr["SendTime"].ToString() != "")
            {
                model.SendTime = DateTime.Parse(dr["SendTime"].ToString());
            }
            if (dr["RecvNO"] != null && dr["RecvNO"].ToString() != "")
            {
                model.RecvNO = dr["RecvNO"].ToString();
            }
            if (dr["MsgContent"] != null && dr["MsgContent"].ToString() != "")
            {
                model.MsgContent = dr["MsgContent"].ToString();
            }
        }
        #endregion  Method
    }
    /// <summary>
    /// 数据访问类:sms_MsgRecive
    /// </summary>
    public partial class sms_MsgRecive
    {
        public sms_MsgRecive()
        { }
        #region  Method
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Slja.GSMUtility.Model.sms_MsgRecive model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into sms_MsgRecive(");
            strSql.Append("Status,SendNO,RecvTime,MsgIndex,MsgContent,Code,ReadMemo)");
            strSql.Append(" values (");
            strSql.Append("@Status,@SendNO,@RecvTime,@MsgIndex,@MsgContent,@Code,@ReadMemo)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Status", SqlDbType.TinyInt,1),
					new SqlParameter("@SendNO", SqlDbType.NVarChar,20),
					new SqlParameter("@RecvTime", SqlDbType.DateTime),
					new SqlParameter("@MsgIndex", SqlDbType.Int,4),
					new SqlParameter("@MsgContent", SqlDbType.NVarChar,1000),
					new SqlParameter("@Code", SqlDbType.NVarChar,20),
					new SqlParameter("@ReadMemo", SqlDbType.NVarChar,2000)};
            parameters[0].Value = model.Status;
            parameters[1].Value = model.SendNO;
            parameters[2].Value = model.RecvTime;
            parameters[3].Value = model.MsgIndex;
            parameters[4].Value = model.MsgContent;
            parameters[5].Value = model.Code;
            parameters[6].Value = model.ReadMemo;

            object obj = Slja.DBUtility.SqlServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 增加多条数据
        /// </summary>
        public List<int> Add(List<Slja.GSMUtility.Model.sms_MsgRecive> modellist)
        {
            StringBuilder strSql = new StringBuilder("declare @t table(id int);");
            SqlParameter[] parameters = new SqlParameter[modellist.Count * 7];
            for (int i = 0; i < modellist.Count; i++)
            {
                Slja.GSMUtility.Model.sms_MsgRecive model = modellist[i];              
                strSql.Append("insert into sms_MsgRecive(");
                strSql.Append("Status,SendNO,RecvTime,MsgIndex,MsgContent,Code,ReadMemo)");
                strSql.Append(" values (");
                strSql.AppendFormat("@Status{0},@SendNO{0},@RecvTime{0},@MsgIndex{0},@MsgContent{0},@Code{0},@ReadMemo{0});",i.ToString());
                strSql.Append("insert into @t(id) select @@identity;");             
              
                (parameters[0 + i * 7] = new SqlParameter("@Status"+i.ToString(), SqlDbType.TinyInt, 1)).Value = model.Status;
                (parameters[1 + i * 7] = new SqlParameter("@SendNO" + i.ToString(), SqlDbType.NVarChar, 20)).Value = model.SendNO;
                (parameters[2 + i * 7] = new SqlParameter("@RecvTime" + i.ToString(), SqlDbType.DateTime)).Value = model.RecvTime;
                (parameters[3 + i * 7] = new SqlParameter("@MsgIndex" + i.ToString(), SqlDbType.Int, 4)).Value = model.MsgIndex;
                (parameters[4 + i * 7] = new SqlParameter("@MsgContent" + i.ToString(), SqlDbType.NVarChar, 1000)).Value = model.MsgContent;
                (parameters[5 + i * 7] = new SqlParameter("@Code" + i.ToString(), SqlDbType.NVarChar, 20)).Value = model.Code;
                (parameters[6 + i * 7] = new SqlParameter("@ReadMemo" + i.ToString(), SqlDbType.NVarChar, 2000)).Value = model.ReadMemo;
            }
            strSql.Append(" select [id] from @t;");
            DataSet ds = Slja.DBUtility.SqlServerHelper.Query(strSql.ToString(), parameters);
            List<int> ListID = new List<int>();
            for (int k = 0; k < ds.Tables[0].Rows.Count; k++)
            {
                ListID.Add((int)ds.Tables[0].Rows[k]["id"]);
            }
            return ListID;
        }
        public int Read(int ID,string UserName)
        {
            string sql = "update dbo.sms_MsgRecive set Status=1,ReadMemo=ReadMemo+'|'+@UserName+'*'+cast(getdate() as varchar(20)) where ID=@ID";
            SqlParameter[] parms = new SqlParameter[2];
            (parms[0]=new SqlParameter("@ID",SqlDbType.Int,4)).Value = ID;
            (parms[1]=new SqlParameter("@UserName",SqlDbType.NVarChar,20)).Value = UserName;
            return Slja.DBUtility.SqlServerHelper.ExecuteSql(sql, parms);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Slja.GSMUtility.Model.sms_MsgRecive model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update sms_MsgRecive set ");
            strSql.Append("Status=@Status,");
            strSql.Append("SendNO=@SendNO,");
            strSql.Append("RecvTime=@RecvTime,");
            strSql.Append("MsgIndex=@MsgIndex,");
            strSql.Append("MsgContent=@MsgContent,");
            strSql.Append("Code=@Code,");
            strSql.Append("ReadMemo=@ReadMemo");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Status", SqlDbType.TinyInt,1),
					new SqlParameter("@SendNO", SqlDbType.NVarChar,20),
					new SqlParameter("@RecvTime", SqlDbType.DateTime),
					new SqlParameter("@MsgIndex", SqlDbType.Int,4),
					new SqlParameter("@MsgContent", SqlDbType.NVarChar,100),
					new SqlParameter("@Code", SqlDbType.NVarChar,20),
					new SqlParameter("@ReadMemo", SqlDbType.NVarChar,2000),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.Status;
            parameters[1].Value = model.SendNO;
            parameters[2].Value = model.RecvTime;
            parameters[3].Value = model.MsgIndex;
            parameters[4].Value = model.MsgContent;
            parameters[5].Value = model.Code;
            parameters[6].Value = model.ReadMemo;
            parameters[7].Value = model.ID;

            int rows = Slja.DBUtility.SqlServerHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Slja.GSMUtility.Model.sms_MsgRecive GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 ID,Status,SendNO,RecvTime,MsgIndex,MsgContent,Code,ReadMemo from dbo.sms_MsgRecive ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            Slja.GSMUtility.Model.sms_MsgRecive model = new Slja.GSMUtility.Model.sms_MsgRecive();
            DataSet ds = Slja.DBUtility.SqlServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                AssignModel(ds.Tables[0].Rows[0], model);
                return model;
            }
            else
            {
                return null;
            }
        }
        public List<Slja.GSMUtility.Model.sms_MsgRecive> GetNewMsg()
        {
            return GetModelList(100,"Status=0");
        }
        public  List<Slja.GSMUtility.Model.sms_MsgRecive> GetModelList(int count,string strWhere)
        {
            int Count = 5;
            if (count != 0)
            {
                Count = count;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top("+Count.ToString()+") ID,Status,SendNO,RecvTime,MsgIndex,MsgContent,Code,ReadMemo from dbo.sms_MsgRecive ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" where ");
                strSql.Append(strWhere);
            }
            strSql.Append(" order by RecvTime desc");
            
            List<Slja.GSMUtility.Model.sms_MsgRecive> list = new List<Model.sms_MsgRecive>();
            DataSet ds = Slja.DBUtility.SqlServerHelper.Query(strSql.ToString());
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Slja.GSMUtility.Model.sms_MsgRecive model = new Model.sms_MsgRecive();
                AssignModel(ds.Tables[0].Rows[i], model);
                list.Add(model);
            }
            return list;
        }
        private void AssignModel(DataRow dr, Slja.GSMUtility.Model.sms_MsgRecive model)
        {
            if (dr["ID"] != null && dr["ID"].ToString() != "")
            {
                model.ID = int.Parse(dr["ID"].ToString());
            }
            if (dr["Status"] != null && dr["Status"].ToString() != "")
            {
                model.Status = int.Parse(dr["Status"].ToString());
            }
            if (dr["SendNO"] != null && dr["SendNO"].ToString() != "")
            {
                model.SendNO = dr["SendNO"].ToString();
            }
            if (dr["RecvTime"] != null && dr["RecvTime"].ToString() != "")
            {
                model.RecvTime = DateTime.Parse(dr["RecvTime"].ToString());
            }
            if (dr["MsgIndex"] != null && dr["MsgIndex"].ToString() != "")
            {
                model.MsgIndex = int.Parse(dr["MsgIndex"].ToString());
            }
            if (dr["MsgContent"] != null && dr["MsgContent"].ToString() != "")
            {
                model.MsgContent = dr["MsgContent"].ToString();
            }
            if (dr["Code"] != null && dr["Code"].ToString() != "")
            {
                model.Code = dr["Code"].ToString();
            }
            if (dr["ReadMemo"] != null && dr["ReadMemo"].ToString() != "")
            {
                model.ReadMemo = dr["ReadMemo"].ToString();
            }
        }
        #endregion  Method
    }
   
   
}